package com.hc.java.sync;

/**
 * @author cdhuangchao3
 * @date 2023/12/2 3:23 PM
 */
public class 死锁 {

    static  final  Object obj = new Object();
    static  final  Object obj2 = new Object();
    public static void method1(){
        synchronized (obj){
            // 同步块
            System.out.println(111);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            synchronized (obj2) {
                System.out.println("111-obj2");
            }
        }
    }

    public static void method2(){
        synchronized (obj2) {
            System.out.println(222);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            synchronized (obj) {
                System.out.println("222-obj");
            }
        }
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(死锁::method1);
        Thread t2 = new Thread(死锁::method2);
        t1.start();
        t2.start();
    }
}
